package org.japo.java.matisse.samples;

import org.japo.java.basics.entities.Nif;

public class CalcularNifAsistido extends javax.swing.JFrame {

    public CalcularNifAsistido() {
        // Antes de la construcción del interfaz
        iniciarAnterior();

        // Construcción del interfaz
        initComponents();

        // Después de la construcción del interfaz
        iniciarPosterior();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txfDni = new javax.swing.JTextField();
        lblGuion = new javax.swing.JLabel();
        lblLetra = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("NIF");
        setResizable(false);

        txfDni.setFont(new java.awt.Font("Calibri", 1, 48)); // NOI18N
        txfDni.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txfDni.setText("99999999");
        txfDni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfDniActionPerformed(evt);
            }
        });

        lblGuion.setFont(new java.awt.Font("Calibri", 1, 48)); // NOI18N
        lblGuion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblGuion.setText("-");

        lblLetra.setFont(new java.awt.Font("Calibri", 1, 48)); // NOI18N
        lblLetra.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblLetra.setText("*");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txfDni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblGuion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblLetra, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txfDni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblLetra)
                    .addComponent(lblGuion))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txfDniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfDniActionPerformed
        procesarNif();
    }//GEN-LAST:event_txfDniActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CalcularNifAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CalcularNifAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CalcularNifAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CalcularNifAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CalcularNifAsistido().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblGuion;
    private javax.swing.JLabel lblLetra;
    private javax.swing.JTextField txfDni;
    // End of variables declaration//GEN-END:variables

    private void iniciarAnterior() {
    }

    private void iniciarPosterior() {
    }

    public void procesarNif() {
        try {
            // Obtener DNI del interfaz
            int dni = Integer.parseInt(txfDni.getText());

            // Calcular la letra del DNI
            char letra = Nif.calcularLetraNif(dni);

            // Poner letra en el interfaz
            lblLetra.setText(letra + "");
        } catch (Exception e) {
            // Marcar la letra como errónea
            lblLetra.setText("*");
        }
    }
}
